/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "rsrinfo.h"

RsrInfo::RsrInfo(int robotId)
{
    //md5Instance = MD5Check::getInstance();
    QString tmpFilePath=D_TCRCONTROLLER_DATA_PATH;
    filePath = tmpFilePath+ROBOT_COMMON_NAME + QString::number(robotId) + CONFIG_STRING + RSR_CONFIG_NAME;

    rsrRobotId = robotId;

#if MD5CHECK_DEBUG
    if(md5Instance->compareMd5(filePath,rsrRobotId))
    {
         readRsrInfo(filePath);
    }
    else
    {

    }
#else
    readRsrInfo(filePath);
#endif
}

bool RsrInfo::readRsrInfo(QString filePath)
{
    bool ok=openXml(filePath, QFile::ReadOnly|QFile::Text );
    if(!ok){
        qDebug()<<"ERROR: openXml :"<<ok<< filePath;
        return false;
    }

    QDomNodeList rsrNodes = m_root.childNodes();
    rsrInfoList.resize( rsrNodes.size() ) ;
    for(int i=0;i<rsrNodes.size();i++)
    {
        QDomNode rsrND =  rsrNodes.at(i);

        QString rsrStr = QString("%1%2").arg("Rsr").arg(i+1);
//        qDebug() << "====rsrStr==="<<rsrStr<<"\n\n\n";
        if( rsrND.nodeName() .indexOf("#comment")>=0){
            continue;
        }

       RsrInformation rsrInfo;

       rsrInfo.num = rsrND.toElement().attribute("id").toInt();
       rsrInfo.diNum = rsrND.toElement().attribute("diNum").toInt();
       rsrInfo.programName = rsrND.toElement().attribute("programName");
       rsrInfo.rsrType = (RSR_TYPE)rsrND.firstChild().nodeValue().toInt();
       rsrInfo.runFlag = 0;

//       qDebug() << "==i==="<<rsrInfo.num<<rsrInfo.diNum<<rsrInfo.programName<<rsrInfo.rsrType<<"\n\n\n";

       rsrInfoList[i] = rsrInfo;
    }

    return true;
}


bool RsrInfo::writeRsrInfo()
{
    bool ok=openXml(filePath, QFile::ReadWrite|QFile::Text );
    if(!ok){
        qDebug()<<"ERROR: openXml :"<<ok<< filePath;
        return false;
    }

    QString rootName = "RsrInfo";
    QString nodeName;
    QString nodeValue;
    QString nodeAttr1 = "id";
    QString nodeAttrValue1;
    QString nodeAttr2 = "programName";
    QString nodeAttrValue2;
    QString nodeAttr3 = "diNum";
    QString nodeAttrValue3;

   for(int i = 0; i < rsrInfoList.size();i++)
   {
       nodeName = QString("%1%2").arg("Rsr").arg(i+1);
       nodeValue = QString::number((int)rsrInfoList[i].rsrType);
       nodeAttrValue1 = QString::number(rsrInfoList[i].num);
       nodeAttrValue2 = rsrInfoList[i].programName;
       nodeAttrValue3 = QString::number(rsrInfoList[i].diNum);

       writeXml(rootName,nodeName,nodeValue,nodeAttr1,nodeAttrValue1,nodeAttr2,nodeAttrValue2,nodeAttr3,nodeAttrValue3);
   }

   closeXml(true);

#if MD5CHECK_DEBUG
   md5Instance->saveFile(filePath,rsrRobotId);
#endif
}
